package com.h3c.nem.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.h3c.nem.dto.SdEmailTemplateDTO;
import com.h3c.nem.entity.SdEmailTemplate;
import com.h3c.nem.mapper.SdEmailTemplateMapper;
import com.h3c.nem.service.CommService;
import com.h3c.nem.service.ISdEmailTemplateService;
import com.h3c.nem.utils.AppBusinessException;
import com.h3c.nem.utils.CommonUtil;
import lombok.extern.slf4j.Slf4j;
import org.jsoup.Jsoup;
import org.jsoup.safety.Safelist;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 实现类
 *
 * @author chenyifan
 * @since 2022-06-14 16:44:35
 */
@Service
@Slf4j
public class SdEmailTemplateServiceImpl extends ServiceImpl<SdEmailTemplateMapper, SdEmailTemplate> implements ISdEmailTemplateService {

    @Resource
    private SdEmailTemplateMapper sdEmailTemplateMapper;

    @Autowired
    private CommService commService;

    /**
     * 模糊查询操作
     */
    @Override
    public List<SdEmailTemplateDTO> selectFuzzyByDto(SdEmailTemplateDTO dto) {
        List<SdEmailTemplate> list = sdEmailTemplateMapper.queryListByFuzzy(JSONObject.parseObject(JSONObject.toJSONString(dto), SdEmailTemplate.class));
        return JSONArray.parseArray(JSONArray.toJSONString(list), SdEmailTemplateDTO.class);
    }

    /**
     * 插入操作
     */
    @Override
    public Boolean insertDto(SdEmailTemplateDTO dto) {
        String mailContent = dto.getEmailTemplateContent();
//        if (StringUtils.hasText(mailContent)) {
//            String content = mailContent.toLowerCase();
//            if (CommonUtil.checkHtml(content)) {
//                throw new AppBusinessException("模板存在非法内容，不能保存");
//            }
//        }
//        String clean = Jsoup.clean(mailContent, Safelist.relaxed());
        log.info("mailContent = {}", mailContent);
        dto.setEmailTemplateContent(mailContent);
        dto.setUpdateTime(new Date());
        dto.setUpdatorInfo(commService.getLoginUserNameAndId());
        return saveOrUpdate(JSONObject.parseObject(JSON.toJSONString(dto), SdEmailTemplate.class));
    }

    @Override
    public Integer updateDto(SdEmailTemplateDTO dto) {
        return sdEmailTemplateMapper.updateSdEmailTemplate(JSONObject.parseObject(JSON.toJSONString(dto), SdEmailTemplate.class));
    }

    /**
     * 删除操作
     */
    @Override
    public Integer deleteDto(SdEmailTemplateDTO dto) {
        return sdEmailTemplateMapper.deleteById(dto.getEmailTemplateId());
    }


}

